<?php


namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\Order as orderModel;
use app\api\service\Order;
use app\api\service\Token;
use app\api\validate\IDMustBePostivelnt;
use app\api\validate\orderPlace;
use app\api\validate\PagingParameter;
use app\lib\enum\ScopeEnum;
use app\lib\exception\ForbiddenException;
use app\lib\exception\TokenException;
//use app\api\service\order as orderService;

class OrderController extends BaseController {
    //用户咋选择商品后，向API提交包含它所选择商品的相关信息
    //API在接收到信息后，需要检查订单相关商品的库存量
    //有库存，把订单数量存入数据库中 = 下单成功，返回客户消息，告诉客户端可以支付了
    //调用我们的支付接口，进行支付
    //支付之前也要进行库存量的检测
    //服务器这边就可以调用微信支付接口进行支付
    //小程序根据服务器返回的结果拉起微信支付
    //微信会返回给我们一个支付的结果(异步)
    //支付到支付成功之间有一个进行库存量的检测
    //成功：进行库存量的扣除，失败：返回一个支付失败的结果
    //成功：进行库存量的扣除

    //前置方法，做权限的验证
    protected  $beforeActionList = [
        'checkExclusiveScope'=>['only'=>'placeOrder']
    ];

    public function placeOrder(){
        (new orderPlace())->goCheck();
        $products = input('post.products/a');
        $uid = Token::getCurrentUid();
        $orderModel = new Order();
        $list = $orderModel->place($uid,$products);
        return json($list);
    }

    //获取用户的订单列表
    public function getSummaryByUser($page = 1,$psize = 5){
        (new PagingParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $pagingOrders = orderModel::getOrderlist($uid,$page,$psize);
        if ($pagingOrders->isEmpty())
        {
            return json([
                'page' => $pagingOrders->currentPage(),
                'data' => []
            ]);
        }
        $data = $pagingOrders->hidden(['snap_items', 'snap_address'])->toArray();
        return json([
            'page' => $pagingOrders->currentPage(),
            'data' => $data
        ]);
    }

    /**
     * 获取全部订单简要信息（分页）
     * @param int $page
     * @param int $size
     * @return array
     * @throws \app\lib\exception\ParameterException
     */
    public function getSummary($page=1, $size = 10){
        (new PagingParameter())->goCheck();
        $pagingOrders = OrderModel::getSummaryByPage($page, $size);
        if ($pagingOrders->isEmpty())
        {
            return json(  [
                'current_page' => $pagingOrders->currentPage(),
                'data' => []
            ]);
        }
        $data = $pagingOrders->hidden(['snap_items', 'snap_address'])
            ->toArray();
        return json([
            'current_page' => $pagingOrders->currentPage(),
            'data' => $data
        ]);
    }


    //点击发货
    public function delivery($id){
        (new IDMustBePostivelnt())->goCheck();
        $order = new Order();
        $success = $order->delivery($id);
//        if($success){
//            return new SuccessMessage();
//        }
    }



}







